package com.zs.toolbox.web2j.system.core.controller.auth;


import com.zs.toolbox.web2j.common.response.WtBaseResponse;
import com.zs.toolbox.web2j.common.web.WtAuthContext;
import com.zs.toolbox.web2j.common.web.WtBaseController;
import com.zs.toolbox.web2j.common.web.WtCommonPropertiesSetter;
import com.zs.toolbox.web2j.system.core.entity.WtUserDetailEntity;
import com.zs.toolbox.web2j.system.core.service.WtUserDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import static com.zs.toolbox.web2j.common.exception.WtResponses.SUCCESS;

/**
 * <p>
 * 用户基本信息操作
 * </p>
 *
 * @author eric.zhang
 * @since 2018-09-21
 */
@RestController
@RequestMapping("/api/v1/user/detail")
@Api(value = "用户详情管理",description = "用户详情管理")
@Slf4j
public class WtUserDetailController extends WtBaseController {

    @Autowired
    private WtUserDetailService wtUserDetailService;

    @ApiOperation("用户详情保存")
    @RequestMapping(value = "/save", method = {RequestMethod.POST, RequestMethod.GET})
    @PreAuthorize("isAuthenticated()")
    WtBaseResponse<String> saveUserDetail(WtUserDetailEntity wtUserDetailEntity) {
        Long userId = WtAuthContext.getUserId();
        WtCommonPropertiesSetter.setDefaultCommonProperty(wtUserDetailEntity);
        wtUserDetailEntity.setId(userId);
        wtUserDetailEntity.setUserId(userId);
        wtUserDetailService.saveOrUpdate(wtUserDetailEntity);
        return SUCCESS;
    }

    @ApiOperation("用户详情查询")
    @RequestMapping(value = "/get", method = {RequestMethod.POST, RequestMethod.GET})
    @PreAuthorize("isAuthenticated()")
    WtBaseResponse<WtUserDetailEntity> saveUserDetail() {
        WtUserDetailEntity entity = wtUserDetailService.getById(WtAuthContext.getUserId());
        return new WtBaseResponse<WtUserDetailEntity>(entity);
    }
}
